package com.wdd.atcrowdfunding.manager.controller;

import com.wdd.atcrowdfunding.bean.Advertisement;
import com.wdd.atcrowdfunding.bean.Cert;
import com.wdd.atcrowdfunding.bean.Member;
import com.wdd.atcrowdfunding.bean.User;
import com.wdd.atcrowdfunding.manager.service.AdvertisementService;
import com.wdd.atcrowdfunding.manager.service.UserService;
import com.wdd.atcrowdfunding.util.AjaxResult;
import com.wdd.atcrowdfunding.util.Page;
import com.wdd.atcrowdfunding.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/auth_adv")
public class authAdvController {

    @Autowired
    private AdvertisementService advertisementService;
    @Autowired
    private UserService userService;

    @RequestMapping("/index.htm")
    public String index(){
        return "advertReview/auth_adv";
    }

    /**
     * 异步加载数据
     * @param pageno
     * @param pagesize
     * @param queryText
     * @return
     */
    @RequestMapping("/doIndex")
    @ResponseBody
    public AjaxResult doIndex(@RequestParam(value = "pageno",required = false,defaultValue = "1") Integer pageno,
                              @RequestParam(value = "pagesize",required = false,defaultValue = "10")Integer pagesize,
                              String queryText){

        AjaxResult ajaxResult = new AjaxResult();
        try{
            Map<String,Object> paramMap = new HashMap();
            paramMap.put("pageno",pageno);
            paramMap.put("pagesize",pagesize);
            if(StringUtil.isNotEmpty(queryText)){
                if (queryText.contains("%")){
                    queryText = queryText.replaceAll("%","\\\\%");
                }
                if (queryText.contains("_")){
                    queryText = queryText.replaceAll("_","\\\\_");
                }
                paramMap.put("queryText",queryText);
            }
            Page<Advertisement> page = advertisementService.pageQuery(paramMap);
            ajaxResult.setSuccess(true);
            ajaxResult.setPage(page);
        }catch (Exception e){
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("查询数据失败");
        }
        return ajaxResult;
    }


    /**
     * 查看详细资料
     * @param userid
     * @param model
     * @return
     */
    @RequestMapping("/selectDetail.htm")
    public String selectDetail(Integer id,Integer userid, Model model){

        User user = userService.getUserById(userid);
        Advertisement advertisement = advertisementService.selectAdvert(id);
        model.addAttribute("user",user);
        model.addAttribute("advertisement",advertisement);

        return "advertReview/selectDetail";
    }



    /**
     * 改变广告状态
     * @param id
     * @param status
     * @return
     */
    @RequestMapping("/upStatus")
    @ResponseBody
    public AjaxResult upStatus(Integer id,String status){

        AjaxResult ajaxResult = new AjaxResult();
        try{
            Advertisement advertisement = new Advertisement();
            advertisement.setStatus(status);
            advertisement.setId(id);
            advertisementService.updateAdvert(advertisement);
            ajaxResult.setSuccess(true);
        }catch (Exception e){
            e.printStackTrace();
            ajaxResult.setSuccess(false);
        }
        return ajaxResult;
    }


}
